package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/download")
public class DownloadController {
    /**
     * author: WheelChen
     * date: 2017/8/11
     * function: 下载导入失败数据excel表
     *
     * @param name 文件名
     * @return
     */
    @RequestMapping(value = "/errorData", method = RequestMethod.POST)
    public ResponseEntity<byte[]> errorData(@RequestBody String name, HttpServletRequest request){
        //获取当前存储excel
        String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static/temp");
        System.out.println(name);
        String filename = savePath + "/" + name;
        System.out.println(filename);
        File file = new File(filename);
        try {
            byte[] data = FileUtils.readFileToByteArray(file);
            HttpHeaders headers = new HttpHeaders();
            filename = new String("导入失败数据".getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * author: WheelChen
     * date: 2017/8/11
     * function: 下载学生数据上传模板
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/StudentTemp")
    public ResponseEntity<byte[]> StudentTemp(HttpServletRequest request){
        String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static");
        String name = "学生数据上传模板.xlsx";
        String url = savePath + "/" + name ;
        File file = new File(url);
        try {
            byte[] data = FileUtils.readFileToByteArray(file);
            HttpHeaders headers = new HttpHeaders();
            String filename = new String(name.getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置MIME类型 以下载方式
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * author: WheelChen
     * date: 2017/10/5
     * function: 下载教师数据上传模板
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/TeacherTemp")
    public ResponseEntity<byte[]> TeacherTemp(HttpServletRequest request){
        String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static");
        String name = "教师数据上传模板.xlsx";
        String url = savePath + "/" + name ;
        File file = new File(url);
        try {
            byte[] data = FileUtils.readFileToByteArray(file);
            HttpHeaders headers = new HttpHeaders();
            String filename = new String(name.getBytes("UTF-8"), "iso-8859-1");
            headers.setContentDispositionFormData("attachment", filename);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置MIME类型 以下载方式
            HttpStatus statusCode = HttpStatus.OK;
            return new ResponseEntity<byte[]>(data, headers, statusCode);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
